var util = require('../../../../utils/util.js');
var bqgUtil = require('../../../../utils/biqugeUtil.js');
var WxParse = require('../../../../wxParse/wxParse.js');
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    novel:{
      title:"--",
      author:"--",
      type:"--",
      updateTime:"更新：--",
      popularity:"人气：--",
      intro: "加载中...",
      img:"http://bmob-cdn-18785.b0.upaiyun.com/2018/05/14/021cf945404acb718050bbe14f805b61.jpg",
      currChArr:[]
    },
    page: 1,
    pageSize: 20,
    pageArray:[],
    total:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    if(options.url){
      that.getNovel(options.url, options.data);
    }else{
      that.getNovel('https://api.biquge.bukaa.cn/19_19273/');
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that = this;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  getNovel: function(url, data) {
    var that = this;
    wx.showLoading({ title: '加载中' });
    console.log("开始发起网络请求");
    var method = "GET";
    var header = "";
    if(data){
        //从搜索页面直接进入
      method = "POST";
      header = { 'Content-Type': 'application/x-www-form-urlencoded' };
      data = JSON.parse(data);
      console.log("url:" + url);
      console.log(data);
    }
    wx.request({
      url: url,
      method: method,
      header: header,
      data: data,
      success: function (res) {
        console.log("成功获取远程内容：" + res.data);
        if (res.statusCode == 200) {
          //解析小说主页
          var novel = bqgUtil.parseNovel(res.data, that);
          console.log("数据成功解析，即将进行数据切割");
          var pageSize = that.data.pageSize;
          if(novel){
            if (novel.allChArr.length > pageSize){
              novel['currChArr'] = novel.allChArr.slice(0,pageSize)
            }
            var page = Math.ceil(novel.allChArr.length / pageSize);
            var pageArray = [];
            for(var i=0; i<page; i++){
              pageArray.push(i);
            }
            that.setData({ novel: novel, total: page, pageArray: pageArray});
          }
        }
      },
      error: function (e) {
        console.log("请求失败:" + JSON.stringify(e));
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  startReading: function(){
    var url = that.data.novel.allChArr[0]['href'];
    wx.navigateTo({
      url: '/pages/my/novel/chapter/chapter?url=' + url,
    })
  },
  showChapter: function(e){
    var url = e.currentTarget.dataset.href;
    wx.navigateTo({
      url: '/pages/my/novel/chapter/chapter?url=' + url,
    })
  },
  changePage: function(e){
    this.setData({
      page: e.detail.value
    })
  },

  prevpage: function(){
    var page = that.data.page;
    var pageSize = that.data.pageSize;
    if (page == 1){
      console.log("到顶了");
      return;
    }
    wx.showLoading({title: '加载中'});
    page--;
    var novel = that.data.novel;
    novel['currChArr'] = novel.allChArr.slice(pageSize * (page - 1), pageSize * page)
    that.setData({ novel: novel, page: page });
    wx.hideLoading();
  },
  nextpage: function(){
    var page = that.data.page;
    var pageSize = that.data.pageSize;
    if (page == that.data.total) {
      console.log("到底了");
      return;
    }
    wx.showLoading({ title: '加载中' });
    page++;
    var novel = that.data.novel;
    novel['currChArr'] = novel.allChArr.slice(pageSize * (page - 1), pageSize*page)
    that.setData({novel:novel, page: page});
    wx.hideLoading();
  },

  imgErr: function(){
    var novel = that.data.novel;
    novel['img'] = "http://bmob-cdn-18785.b0.upaiyun.com/2018/05/14/021cf945404acb718050bbe14f805b61.jpg";
    that.setData({novel: novel});
  },
})